import 'package:amoy_ticket/controller/event/event_declration.dart';
import 'package:amoy_ticket/controller/event/movie_tabbar_two_event.dart';
import 'package:amoy_ticket/movie/movie_tabbar_two/movie_tabbar_two_city.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import 'movie_tabbar_two_screen.dart';

class MovieTabBarTwo extends StatefulWidget {
  MovieTabBarTwo({Key? key}) : super(key: key);

  @override
  _MovieTabBarTwoState createState() => _MovieTabBarTwoState();
}

class _MovieTabBarTwoState extends State<MovieTabBarTwo>
    with SingleTickerProviderStateMixin {
  List movieTabTwoList = [
    {
      'name': '南京中影国际影城(河西奥体店)',
      'praiseDegree': '90',
      'position': '建邺区江东中路341号',
      'distance': '1.0',
      'price': '21.9',
      'label': '影城卡特惠￥30,特惠票,艺术影厅,激光厅,可停车',
      'beforePrice': '24',
    },
    {
      'name': 'CGV影城(华采天地IMAX店)',
      'praiseDegree': '96',
      'position': '建邺区江东中路258号华采天地5楼',
      'distance': '1.0',
      'price': '32.9',
      'label': '影城卡特惠￥9.9,特惠票,可改签,观影小票,IMAX厅,融合厅,可停车',
      'beforePrice': '33',
    },
    {
      'name': '南京喜满客影城(缤润汇店)',
      'praiseDegree': '95',
      'position': '建邺区楠溪江东街83号缤润汇购物中心3楼',
      'distance': '1.2',
      'price': '23',
      'label': '影城卡,特惠票,可退票,可改签,观影小食,可停车',
    },
    {
      'name': '幸福蓝海国际影城(河西店)',
      'praiseDegree': '91',
      'position': '建邺区江东中路237号雨润国际广场中央商场5楼',
      'distance': '2',
      'price': '27.4',
      'label': '影城卡,特惠票,可改签,MX4D厅,DTS临境音,可停车',
      'beforePrice': '30',
    },
    {
      'name': '江苏大剧院水滴影院',
      'praiseDegree': '96',
      'position': '建邺区梦都大街181号江苏大剧院中庭',
      'distance': '2.6',
      'price': '22.9',
      'label': '影城卡,特惠票,可退票,可改签,观影小食,杜比全景声厅,VIP厅,可停车',
      'beforePrice': '35',
    },
    {
      'name': '银河换了影城(油坊桥旺角广场4D店)',
      'praiseDegree': '',
      'position': '建邺区油坊桥地铁站3.5出口旺角广场3楼',
      'distance': '2.7',
      'price': '24.5',
      'label': '3D眼镜收费,4D厅,可停车',
    },
    {
      'name': '上影国际影城西善桥店',
      'praiseDegree': '91',
      'position': '雨花台区西善桥街道龙淮路15号1栋4楼',
      'distance': '4.1',
      'price': '23',
      'label': '可退票,可改签,观影小食,可停车',
    },
    {
      'name': '卢米埃南京金鹰河西影城',
      'praiseDegree': '95',
      'position': '建邺区应天大街888号金鹰世界9楼',
      'distance': '4.2',
      'price': '31.9',
      'label': '影城卡,特惠票,可改签,观影小食,杜比全息声厅,realD厅',
      'beforePrice': '38',
    },
    {
      'name': '南京楚翘城体验影城',
      'praiseDegree': '89',
      'position': '雨花台区安德门大街57号楚翘城3幢501',
      'distance': '4.8',
      'price': '33',
      'label': '可改签,VIP厅,可停车',
    },
    {
      'name': 'AMG海上明珠影城(南京虹悦城realDCinema店)',
      'praiseDegree': '94',
      'position': '雨花台区应天大街619号德盈广场虹悦城4楼',
      'distance': '4.9',
      'price': '38',
      'label': 'realD厅,4D厅,可停车',
    },
    {
      'name': '万达影城(建邺万达广场IMAX厅)',
      'praiseDegree': '92',
      'position': '建邺区江东中路98号建邺万达广场娱乐楼四楼',
      'distance': '5.1',
      'price': '25.8',
      'label': '影城卡特惠￥19.9,特惠票,可改签,IMAX厅,杜比影院,可停车',
      'beforePrice': '37.9',
    },
    {
      'name': '幸福蓝海悦乐影城(集庆门店)',
      'praiseDegree': '93',
      'position': '建邺区集庆门大街24号悦乐荟3层',
      'distance': '5.5',
      'price': '19.9',
      'label': '限时特价票,可退票,可改签,观影小食',
      'beforePrice': '25.9',
    },
    {
      'name': '中影东方星凯影城',
      'praiseDegree': '',
      'position': '雨花台区岱山北路15号东来荟邻生活广场三楼',
      'distance': '5.5',
      'price': '22.9',
      'label': '影城卡,特惠票,可退票,可改签,可停车',
      'beforePrice': '23',
    },
    {
      'name': '星轶STARX影城(建邺吾悦广场店)',
      'praiseDegree': '',
      'position': '建邺区汉中门大街299号建邺吾悦广场4F',
      'distance': '5.6',
      'price': '19.9',
      'label': '显示特价票',
      'beforePrice': '30',
    },
    {
      'name': '大华大戏院南湖影城',
      'praiseDegree': '93',
      'position': '建邺区南湖路1号',
      'distance': '6.0',
      'price': '20.8',
      'label': '影城卡,特惠票,可改签,3D眼镜收费',
      'beforePrice': '23.9',
    },
    {
      'name': '南京清江苏宁影城',
      'praiseDegree': '94',
      'position': '鼓楼区江东北路88号清江苏宁广场负一楼',
      'distance': '6.6',
      'price': '20.9',
      'label': '影城卡,特惠票,可退票,可改签,观影小食,艺术影厅,可停车',
      'beforePrice': '26.9',
    },
    {
      'name': '南京银兴菲林影城老门东店',
      'praiseDegree': '92',
      'position': '秦淮区上江考棚16、18号',
      'distance': '6.8',
      'price': '21.8',
      'label': '特惠票,VIP厅,艺术影厅,可停车',
      'beforePrice': '33.9',
    },
    {
      'name': '幸福蓝海国际影城(IMAX激光环宇城店)',
      'praiseDegree': '',
      'position': '鼓楼区凤熙路8号中海环宇城3-4楼',
      'distance': '7.0',
      'price': '22.9',
      'label': '影城卡,特惠票,可改签,IMAX厅',
      'beforePrice': '30',
    },
    {
      'name': '横店电影城南京水游城店',
      'praiseDegree': '92',
      'position': '秦淮区建康路1号水游城4楼',
      'distance': '7.4',
      'price': '22.9',
      'label': '可停车',
      'beforePrice': '35.9',
    },
    {
      'name': '南京星轶影城(茂业天地精品店)',
      'praiseDegree': '94',
      'position': '秦淮区建康路2号茂业天地B109',
      'distance': '7.4',
      'price': '35',
      'label': '影城卡特惠￥6.9,可改签,观影小食,杜比全景声厅,艺术影厅',
    },
    {
      'name': '南京中影国际影城(南站店)',
      'praiseDegree': '89',
      'position': '雨花台区玉兰路99号明发商业广场1幢B6区4楼',
      'distance': '7.5',
      'price': '23',
      'label': '影城卡特惠￥30,观影小食,艺术影厅,可停车',
    },
    {
      'name': '幸福蓝海欢享影城(万象都荟店)',
      'praiseDegree': '92',
      'position': '秦淮区永乐路111号,万象都荟商业广场2楼',
      'distance': '7.5',
      'price': '29.5',
      'label': '3D眼镜收费,VIP厅,艺术影厅,可停车',
    },
    {
      'name': '大华大戏院秦淮影城',
      'praiseDegree': '',
      'position': '秦淮区贡院街80号4楼(平江府31号对面)',
      'distance': '7.8',
      'price': '21.4',
      'label': '影城卡,特惠票,可改签,3D眼镜收费,观影小食',
      'beforePrice': '33.9',
    },
    {
      'name': 'SFC上影影城(南京店)',
      'praiseDegree': '95',
      'position': '鼓楼区草场门大街99号新城市置业广场3楼',
      'distance': '8.0',
      'price': '22.9',
      'label': '影城卡特惠￥7.9,特惠票,艺术影厅,可停车',
      'beforePrice': '30',
    },
    {
      'name': '中影喜盟尊享影城(卡子门店)',
      'praiseDegree': '93',
      'position': '秦淮区卡子门大街99号复地活力广场3楼',
      'distance': '8.1',
      'price': '20.9',
      'label': '影城卡,特惠票,可改签,观影小食,VIP厅,可停车',
      'beforePrice': '23',
    },
    {
      'name': '卢米埃南京金鹰新街口影城',
      'praiseDegree': '95',
      'position': '秦淮区汉中路101号金鹰国际购物中心新街口店B座9层',
      'distance': '8.2',
      'price': '26.9',
      'label': '影城卡,特惠票,可改签,观影小食,杜比全景声厅,realD厅,可停车',
      'beforePrice': '30',
    },
    {
      'name': '艾米1895影城(龙江店)',
      'praiseDegree': '92',
      'position': '鼓楼区江东北路319号华彩荟5楼',
      'distance': '8.2',
      'price': '28',
      'label': '影城卡,可改签,观影小食,可停车',
    },
    {
      'name': 'IDA影城(南京保利若比邻堂悦店)',
      'praiseDegree': '',
      'position': '秦淮区汇康路99号14栋1室(1-4商业)保利若比邻堂悦内四楼C401商铺',
      'distance': '8.3',
      'price': '19.9',
      'label': '特惠票,可退票,可改签,可停车',
    },
    {
      'name': '万达影城(新街口杜比影院店)',
      'praiseDegree': '91',
      'position': '秦淮区洪武路88号印象汇广场四楼(沃尔玛楼上)',
      'distance': '8.4',
      'price': '22.9',
      'label': '影城卡特惠￥19.9,特惠票,可改签,杜比影院,120帧,可停车',
      'beforePrice': '29.9',
    },
    {
      'name': '南京大华大戏院',
      'praiseDegree': '91',
      'position': '秦淮区新街口中山南路67号',
      'distance': '8.4',
      'price': '22.4',
      'label': '杜比全景声厅,艺术影厅,可停车',
      'beforePrice': '29.5',
    },
  ];

  ///给获取详细信息的widget设置一个key
  GlobalKey iconkey = GlobalKey();

  ///获取位置，给后续弹窗设置位置
  // Offset iconOffset;
  //是否点击了综合排序
  bool _isSelectComprehensive = false;
  // 默认文字
  String comprehensiveSorting = '综合排序';

  ///获取size 后续计算弹出位置
  // Size iconSize;
  //是否点击了筛选
  bool isClickScreen = false;
  //是否点击了全城
  bool _isCLickCity = false;
  String _cityName = '全城';

  int _currentLeftIndex = 0;
  int _currentRightIndex = 0;
  int _currentTabIndex = 0;
  List _isSelectScreenList = [];

  ///接受弹窗类构造成功传递来的关闭参数
  // Function closeModel;
  List _homeTabTwoSwiperList = [
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/movie_tab_two_swiper/image1.png',
    'https://amoy-ticket.oss-cn-nanjing.aliyuncs.com/images/movie_tab_two_swiper/image1.png',
    // 'http://qyu5iz9il.hd-bkt.clouddn.com/movie_swiper_image3.png',
    // 'http://qyu5iz9il.hd-bkt.clouddn.com/movie_swiper_image4.png'
  ];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Divider(),
        Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    child: Row(
                      children: [
                        Text(
                          _cityName,
                          style: TextStyle(
                              color: _isCLickCity || _cityName != '全城'
                                  ? Colors.pink
                                  : Colors.black),
                        ),
                        Icon(
                          _isCLickCity
                              ? Icons.arrow_drop_up
                              : Icons.arrow_drop_down,
                          size: 13.sp,
                          color: _isCLickCity || _cityName != '全城'
                              ? Colors.pink
                              : Colors.black,
                        ),
                      ],
                    ),
                    onTap: () {
                      setState(() {
                        _isCLickCity = !_isCLickCity;
                      });
                      SmartDialog.compatible.show(
                        alignmentTemp: Alignment.center,
                        isPenetrateTemp: true,
                        maskColorTemp: Colors.transparent,
                        onDismiss: () {
                          setState(() {
                            _isCLickCity = false;
                          });
                          eventBus.on<MovieTabTwoCityEvent>().listen((event) {
                            setState(() {
                              _cityName = event.cityName;
                              _currentLeftIndex = event.currentLeftIndex;
                              _currentRightIndex = event.cuttentRightIndex;
                              _currentTabIndex = event.currentTabIndex;
                            });
                          });
                        },
                        widget: Column(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Container(
                              width: MediaQuery.of(context).size.width,
                              height: 443.h,
                              color: Colors.white,
                              child: MovieTabBarTwoCity(
                                currentLeftIndex: _currentLeftIndex,
                                currentRightIndex: _currentRightIndex,
                                currentTabIndex: _currentTabIndex,
                              ),
                            ),
                            GestureDetector(
                              onTap: () {
                                SmartDialog.dismiss();
                              },
                              child: Container(
                                height: 118.w,
                                color: Colors.black.withOpacity(0.6),
                              ),
                            )
                          ],
                        ),
                      );
                    },
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    child: Row(
                      children: [
                        SizedBox(width: 10.w),
                        Text(
                          '筛选',
                          style: TextStyle(
                              color: isClickScreen ||
                                      _isSelectScreenList.length > 0
                                  ? Colors.pink
                                  : Colors.black),
                        ),
                        Visibility(
                          visible: _isSelectScreenList.length > 0,
                          child: Padding(
                            padding: EdgeInsets.only(left: 2.w),
                            child: Text(
                              _isSelectScreenList.length.toString(),
                              style: TextStyle(color: Colors.pink),
                            ),
                          ),
                        ),
                        Icon(
                          isClickScreen
                              ? Icons.arrow_drop_up
                              : Icons.arrow_drop_down,
                          size: 13.sp,
                          color: isClickScreen || _isSelectScreenList.length > 0
                              ? Colors.pink
                              : Colors.black,
                        ),
                      ],
                    ),
                    onTap: () {
                      setState(() {
                        isClickScreen = !isClickScreen;
                      });
                      SmartDialog.compatible.show(
                          alignmentTemp: Alignment.center,
                          isPenetrateTemp: true,
                          onDismiss: () {
                            setState(() {
                              isClickScreen = false;
                            });

                            eventBus
                                .on<MovieTabTwoScreenEvent>()
                                .listen((event) {
                              _isSelectScreenList = event.isSelectList;
                              print('============$_isSelectScreenList');
                            });
                            // TODO 这地方很邪门 list没有传却存在

                            print(_isSelectScreenList);
                          },
                          widget: MovieTabBarTwoScreen(_isSelectScreenList));
                    },
                  ),
                ],
              ),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                child: Row(
                  children: [
                    SizedBox(width: 10.w),
                    Text(
                      comprehensiveSorting,
                      style: TextStyle(
                          color: _isSelectComprehensive
                              ? Colors.red
                              : Colors.black),
                    ),
                    Icon(
                      _isSelectComprehensive
                          ? Icons.arrow_drop_up
                          : Icons.arrow_drop_down,
                      size: 13.sp,
                      color: _isSelectComprehensive ? Colors.red : Colors.black,
                    ),
                  ],
                ),
                onTap: () {
                  setState(() {
                    _isSelectComprehensive = !_isSelectComprehensive;
                  });
                  SmartDialog.compatible.show(
                    alignmentTemp: Alignment.center,
                    isPenetrateTemp: true,
                    onDismiss: () {
                      setState(() {
                        _isSelectComprehensive = !_isSelectComprehensive;
                        //  comprehensiveSorting
                      });
                    },
                    widget: Stack(
                      children: [
                        Positioned(
                          top: 145.h,
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 10.w),
                            color: Colors.white,
                            width: MediaQuery.of(context).size.width,
                            child: Column(
                              children: [
                                _sortOrder('综合排序'),
                                _sortOrder('离我最近'),
                                _sortOrder('价格最低'),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  );
                },
              ),
            ],
          ),
        ),
        Container(
          margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 5.h),
          height: 80,
          child: Swiper(
            itemCount: _homeTabTwoSwiperList.length,
            loop: true, // 允许循环播放
            autoplay: true, // 允许自动播放
            scrollDirection: Axis.horizontal, // 滚动方向设置为垂直
            autoplayDelay: 3000, // 执行时长
            itemBuilder: (BuildContext context, int index) {
              return CachedNetworkImage(
                imageUrl: _homeTabTwoSwiperList[index],
                fit: BoxFit.fill,
                // color: Colors.white,
              );
            },
          ),
        ),
        Expanded(
          child: ListView.builder(
            itemCount: movieTabTwoList.length,
            itemBuilder: (context, index) {
              return _itemWidget(
                movieTabTwoList[index]?['name'] ?? '',
                movieTabTwoList[index]?['praiseDegree'] ?? '',
                movieTabTwoList[index]?['position'] ?? '',
                movieTabTwoList[index]?['distance'] ?? '',
                movieTabTwoList[index]?['price'] ?? '',
                label: movieTabTwoList[index]?['label'] ?? '',
                beforePrice: movieTabTwoList[index]?['beforePrice'] ?? '',
              );
            },
          ),
        )
      ],
    );
  }

  Widget _sortOrder(String label) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        SmartDialog.dismiss();
        comprehensiveSorting = label;
      },
      child: Container(
        width: MediaQuery.of(context).size.width,
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 5.h),
          child: Row(children: [
            comprehensiveSorting == label
                ? Icon(Icons.done, color: Colors.red)
                : SizedBox(width: 23.w),
            Text(
              label,
              style: TextStyle(
                  color: comprehensiveSorting == label
                      ? Colors.pink
                      : Colors.black),
            )
          ]),
        ),
      ),
    );
  }

  Widget _itemWidget(
    String name,
    String praiseDegree,
    String position,
    String distance,
    String price, {
    String label = '',
    String beforePrice = '',
  }) {
    List labelList = label.split(',');
    return Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6.r), color: Colors.white),
      margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
      padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 15.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                name,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 13.sp,
                ),
              ),
              Row(
                children: [
                  Text(
                    beforePrice,
                    style: TextStyle(
                      decoration: TextDecoration.lineThrough,
                      fontSize: 10.sp,
                    ),
                  ),
                  SizedBox(width: 2.w),
                  Text(
                    '￥$price',
                    style: TextStyle(
                        color: Colors.red,
                        fontSize: 10.sp,
                        fontWeight: FontWeight.bold),
                  ),
                  Text(
                    '起',
                    style: TextStyle(fontSize: 10.sp),
                  ),
                ],
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Row(
                  children: [
                    Text('好评度$praiseDegree%'),
                    Container(
                      height: 10.h,
                      child: VerticalDivider(
                        thickness: 1,
                        color: Colors.grey,
                      ),
                    ),
                    Expanded(
                      child: Text(
                        position,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                    ),
                    SizedBox(width: 30),
                  ],
                ),
              ),
              Text(
                '${distance}km',
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              )
            ],
          ),
          Padding(
            padding: EdgeInsets.only(right: 30.w, left: 0),
            child: Wrap(
              // runSpacing: 10.h,
              spacing: 4.w,
              runSpacing: 4.h,
              alignment: WrapAlignment.start,
              runAlignment: WrapAlignment.start,
              // direction: Axis.vertical,
              children: labelList.map((e) {
                if (e == '特惠票') {
                  return _withBorderContainer('特惠票', color: Color(0xFFee942f));
                } else if (e == '可退票') {
                  return _withBorderContainer('可退票', color: Color(0xFF45a4ee));
                } else if (e == '可改签') {
                  return _withBorderContainer('可退票', color: Color(0xFF429bed));
                } else if (e.toString().contains('影城卡')) {
                  return _withBorderContainer(e, color: Color(0xFF4fbfa1));
                } else {
                  return _withBorderContainer(e);
                }
              }).toList(),
            ),
          )
        ],
      ),
    );
  }

  Widget _withBorderContainer(String text, {Color color = Colors.black}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 1.h),
      child: Text(
        text,
        style: TextStyle(color: color, fontSize: 10.sp),
      ),
      decoration:
          BoxDecoration(border: Border.all(color: Colors.grey.shade200)),
    );
  }
}
